package com.qfedu.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qfedu.dao.DepartDao;
import com.qfedu.entity.Depart;
import com.qfedu.entity.DepartNumDate;
import com.qfedu.entity.ResponseData;

import com.qfedu.service.DepartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class DepartServiceImpl implements DepartService {

    @Autowired
    private DepartDao departDao;

    @Override
    public boolean addDepart(Depart depart) {
        int i = departDao.addDepart(depart);
        if (i > 0){
            return true;
        }
        return false;
    }

    @Override
    public ResponseData<List<Depart>> findAllDepart(int pageIndex, int pageSize) {
        PageHelper.startPage(pageIndex,pageSize);
        List<Depart>departList = departDao.findAllDepart();
        PageInfo pageInfo = new PageInfo(departList);
        int count = (int) pageInfo.getTotal();

        ResponseData<List<Depart>> responseData = new ResponseData<>();
        responseData.setCode(0);
        responseData.setData(departList);
        responseData.setCount(count);
        responseData.setMsg("ok");

        return responseData;


    }

    @Override
    public boolean deleteDepartById(int id) {
        int i = departDao.deleteDepartById(id);
        if( i > 0){
            return true ;
        }
        return false;
    }

    @Override
    public boolean updateDepart(Depart depart) {
        int i = departDao.updateDepart(depart);
        if(i > 0){
            return true;
        }
        return false;
    }

    @Override
    public DepartNumDate getDepartNumData() {

        List<Map> mapList = departDao.getDepartNum();
        List<String> departNameList = new ArrayList<>();
        List<Integer> departNumList = new ArrayList<>();
        for(Map map:mapList){
            System.out.println("map"+map);
            departNameList.add((String) map.get("name"));
            Long lNum = (Long) map.get("num");

            //部门数量
            departNumList.add(lNum.intValue());

        }

        DepartNumDate departNumDate = new DepartNumDate();
        departNumDate.setDepartNameList(departNameList);
        departNumDate.setDepartNumList(departNumList);

        return departNumDate;
    }


}
